package com.edu.ctgu;

import java.util.Map;
import java.util.HashMap;

public class Covid19Detection {
    // 症状枚举
    public enum Symptom {
        FEVER(1, "发热"),
        SORE_THROAT(2, "咽喉痛"),
        COUGH(3, "咳嗽"),
        NASAL_CONGESTION(4, "鼻塞"),
        RUNNY_NOSE(5, "流鼻涕"),
        CHEST_TIGHTNESS(6, "胸闷"),
        SHORTNESS_OF_BREATH(7, "气急"),
        DIFFICULTY_BREATHING(8, "呼吸困难"),
        BODY_ACHE(9, "全身酸痛"),
        FATIGUE(10, "乏力"),
        DIARRHEA(11, "腹泻"),
        NONE(0, "都没有");

        private final int code;
        private final String description;

        Symptom(int code, String description) {
            this.code = code;
            this.description = description;
        }

        public int getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static Symptom fromCode(int code) {
            for (Symptom symptom : values()) {
                if (symptom.code == code) {
                    return symptom;
                }
            }
            return NONE;
        }
    }

    // 风险等级枚举
    public enum RiskLevel {
        HIGH(0, "高风险"),
        MEDIUM(1, "中风险"),
        LOW(2, "低风险"),
        VERY_LOW(3, "极低风险");

        private final int code;
        private final String description;

        RiskLevel(int code, String description) {
            this.code = code;
            this.description = description;
        }

        public int getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static RiskLevel fromCode(int code) {
            for (RiskLevel level : values()) {
                if (level.code == code) {
                    return level;
                }
            }
            throw new IllegalArgumentException("无效的风险等级代码: " + code);
        }
    }

    // 建议消息
    private static final Map<RiskLevel, String> ADVICE_MAP = Map.of(
            RiskLevel.HIGH, "建议立刻发热门诊就诊，并且佩戴好口罩做好防护。佩戴医用外科口罩或N95口罩，避免乘坐公共交通工具。",
            RiskLevel.MEDIUM, "结合你提供的病情症状及接触史特点，建议继续在家监测体温，可适当服用治疗感冒的中成药，注意手卫生，多饮水，休息为主。若体温持续不退或体温大于38℃或不适症状加重，及时就诊。",
            RiskLevel.LOW, "建议你们全家及密切接触者居家隔离至少2周，在家期间建议戴口罩交流，条件允许时，尽量单独居住或居住在通风良好的单人房间。多休息，多饮水，注意手卫生和常用物品卫生消毒。若出现发热及呼吸道症状，需马上前往医院就诊。",
            RiskLevel.VERY_LOW, "建议您戴口罩、勤洗手，避免人群聚集，减少不必要外出。"
    );

    // 症状数据类
    public static class SymptomData {
        private final int[][] data;

        public SymptomData(int[][] data) {
            if (data == null || data.length < 8) {
                throw new IllegalArgumentException("Invalid symptom data structure");
            }
            this.data = data;
        }

        public boolean hasSymptom(Symptom symptom) {
            if (symptom == Symptom.NONE) {
                return data[0][0] == 0;
            }
            for (int i = 0; i < data[0].length; i++) {
                if (data[0][i] == symptom.getCode()) {
                    return true;
                }
                if (data[0][i] == -1) {
                    break;
                }
            }
            return false;
        }

        public boolean hasContactHistory() {
            return data[7][0] != 0;
        }

        public boolean hasRecentDiscomfort() {
            return data[6][0] == 0;
        }

        public float getCurrentTemperature() {
            return data[1][1] / 10.0f;
        }

        public boolean hasHighFever() {
            return data[1][0] == 1;
        }

        public boolean hasPersistentFever() {
            return data[1][2] == 0;
        }

        public boolean hasTakenAntipyretics() {
            return data[1][3] == 0;
        }

        public boolean hasCoughWithPhlegm() {
            return data[2][0] == 1;
        }

        public boolean hasWorsenedChestTightness() {
            return data[3][0] == 1;
        }

        public boolean hasWorsenedShortnessOfBreath() {
            return data[4][0] == 1;
        }

        public boolean hasWorsenedDiarrhea() {
            return data[5][0] == 1;
        }
    }

    public static RiskLevel classify(int[][] inputData) {
        SymptomData data = new SymptomData(inputData);
        StringBuilder report = new StringBuilder();

        // 生成症状报告
        generateSymptomReport(data, report);

        // 确定风险等级
        RiskLevel riskLevel = determineRiskLevel(data);

        // 输出报告和建议
        System.out.println(report.toString());
        System.out.println("\n建议: " + ADVICE_MAP.get(riskLevel));

        return riskLevel;
    }

    private static void generateSymptomReport(SymptomData data, StringBuilder report) {
        report.append("症状检查报告:\n");
        report.append("-------------------------\n");

        if (data.hasSymptom(Symptom.NONE)) {
            report.append("- 无任何症状\n");
        } else {
            // 检查每种症状
            for (Symptom symptom : Symptom.values()) {
                if (symptom != Symptom.NONE && data.hasSymptom(symptom)) {
                    report.append("- ").append(symptom.getDescription()).append("\n");
                    processSpecificSymptom(data, symptom, report);
                }
            }
        }

        // 添加接触史信息
        report.append("接触史: ").append(data.hasContactHistory() ? "有" : "无").append("\n");
        report.append("不适持续时间: ").append(data.hasRecentDiscomfort() ? "14天以内" : "14天以上").append("\n");
    }

    private static void processSpecificSymptom(SymptomData data, Symptom symptom, StringBuilder report) {
        switch (symptom) {
            case FEVER:
                report.append("  当前体温: ").append(data.getCurrentTemperature()).append("℃\n");
                report.append("  发热情况: ").append(data.hasHighFever() ? "≥37.3℃" : "<37.3℃").append("\n");
                report.append("  持续情况: ").append(data.hasPersistentFever() ? "体温持续不退" : "间歇性发热").append("\n");
                report.append("  用药情况: ").append(data.hasTakenAntipyretics() ? "已服用退热药" : "未服用退热药").append("\n");
                break;
            case COUGH:
                report.append("  咳痰情况: ").append(data.hasCoughWithPhlegm() ? "有咳痰" : "无咳痰").append("\n");
                break;
            case CHEST_TIGHTNESS:
                report.append("  胸闷变化: ").append(data.hasWorsenedChestTightness() ? "有加重" : "无加重").append("\n");
                break;
            case SHORTNESS_OF_BREATH:
                report.append("  气急变化: ").append(data.hasWorsenedShortnessOfBreath() ? "有加重" : "无加重").append("\n");
                break;
            case DIARRHEA:
                report.append("  腹泻变化: ").append(data.hasWorsenedDiarrhea() ? "有加重" : "无加重").append("\n");
                break;
            default:
                // 其他症状不需要额外信息
                break;
        }
    }

    private static RiskLevel determineRiskLevel(SymptomData data) {
        if (data.hasSymptom(Symptom.NONE)) {
            return data.hasContactHistory() ? RiskLevel.LOW : RiskLevel.VERY_LOW;
        } else {
            return data.hasContactHistory() ? RiskLevel.HIGH : RiskLevel.MEDIUM;
        }
    }
}